package com.ifast.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.ifast.common.base.CoreServiceImpl;
import com.ifast.common.utils.Const;
import com.ifast.sys.dao.UserGroupDao;
import com.ifast.sys.domain.DeptDO;
import com.ifast.sys.domain.UserGroupChildDO;
import com.ifast.sys.domain.UserGroupDO;
import com.ifast.sys.service.DeptService;
import com.ifast.sys.service.UserGroupChildService;
import com.ifast.sys.service.UserGroupService;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <pre>
 * </pre>
 * <small> 2018年3月23日 | Aron</small>
 */
@Transactional
@Service
public class UserGroupServiceImpl extends CoreServiceImpl<UserGroupDao, UserGroupDO> implements UserGroupService {

    @Autowired
    private UserGroupChildService userGroupChildService;

    @Autowired
    private DeptService deptService;


    @Override
    public Page<UserGroupDO> listPage(Page<UserGroupDO> page, UserGroupDO userGroupDO){
        return page.setRecords(baseMapper.listPage(page,userGroupDO));
    }


    @Override
    public boolean saveUserGroupDO(UserGroupDO userGroupDO){
        List<String> szList = Arrays.stream(Optional.ofNullable(userGroupDO.getUserIds())
                .flatMap(strz->Optional.ofNullable(userGroupDO.getUserIds().split(",")))
                .orElse(new String[]{})).collect(Collectors.toList());
        //新增主表
        userGroupDO.setId(Const.uuid());
        userGroupDO.setCreateTime(DateUtil.now());
        DeptDO deptDO = this.deptService.selectById(userGroupDO.getDeptId());
        userGroupDO.setDwCode(deptDO.getDwCode());
        this.baseMapper.insert(userGroupDO);
        //子表
        List<UserGroupChildDO> doList = Lists.newArrayList();
        uGroupCommom(userGroupDO, szList, doList);
        return this.userGroupChildService.insertBatch(doList);
    }

    /**
     * 公共处理
     * @param userGroupDO
     * @param szList
     * @param doList
     */
    private void uGroupCommom(UserGroupDO userGroupDO, List<String> szList, List<UserGroupChildDO> doList) {
        for (String userId : szList) {
            UserGroupChildDO userGroupChildDO = new UserGroupChildDO();
            userGroupChildDO.setId(Const.uuid());
            userGroupChildDO.setCreateTime(DateUtil.now());
            userGroupChildDO.setUserGroupId(userGroupDO.getId());
            userGroupChildDO.setUserId(userId);
            doList.add(userGroupChildDO);
        }
    }

    @Override
    public boolean updateUserGroupDO(UserGroupDO userGroupDO){
        this.baseMapper.updateById(userGroupDO);
        Wrapper<UserGroupChildDO> wrapper = new EntityWrapper<UserGroupChildDO>();
        wrapper.eq("user_group_Id",userGroupDO.getId());
        this.userGroupChildService.delete(wrapper);
        List<String> szList = Arrays.stream(Optional.ofNullable(userGroupDO.getUserIds())
                .flatMap(strz->Optional.ofNullable(userGroupDO.getUserIds().split(",")))
                .orElse(new String[]{})).collect(Collectors.toList());
        //子表
        List<UserGroupChildDO> doList = Lists.newArrayList();
        uGroupCommom(userGroupDO, szList, doList);
        return this.userGroupChildService.insertBatch(doList);
    }

    @Override
    public boolean delUserGroupDO(String idz){
        if(StrUtil.isBlank(idz)){
            return  false;
        }
        this.baseMapper.deleteById(idz);
        Wrapper<UserGroupChildDO> wrapper = new EntityWrapper<UserGroupChildDO>();
        wrapper.eq("user_group_Id",idz);
        return this.userGroupChildService.delete(wrapper);
    }


}
